<?php

namespace App\Services;

use App\Common\Libs\Ali\Oss;
use App\Consts\GlobalUploadConst;

class AgreementService extends BaseService
{
    /**
     * 获取协议
     * @param string $route
     */
    public function getAgreement($route)
    {
        $agreement = service()->SysAgreementService->getAgreementByRoute($route);
        if (!$agreement) {
            return (object)[];
        }

        // OSS读取content
        $routeUrl = $agreement->url;
        $ossJson = Oss::getInstance(GlobalUploadConst::AGREEMENT_OBJECT_PATH)->getObject($routeUrl);

        // 过滤style
        $content = preg_replace('/style=\".*?\"/', ' ', json_decode($ossJson, true)['content']);

        return [
            'title' => $agreement->title,
            'content' => $content,
        ];
    }
}
